package vsvm.data.source;

import java.util.ArrayList;

import vsvm.data.model.AbstractAttribute;
import vsvm.data.model.AbstractDataModel;
import vsvm.data.model.CategorialAttribute;
import vsvm.data.model.NumericAttribute;
import vsvm.gui.Environment;

import vsvm.gui.forms.ConfigureSVMDataSourceDialog;

public class SVMFileDataSource extends FileDataSource {
	private static final long serialVersionUID = 100L;
	private String delimiter1;
        private String delimiter2;

	public SVMFileDataSource(String filename) {
		this.filename = filename;
		this.delimiter1 = " ";
                this.delimiter2 = ":";
	}
	
	public SVMFileDataSource(String filename, String delimiter1, String delimiter2) {
		this.filename = filename;
		this.delimiter1 = delimiter1;
                this.delimiter2 = delimiter2;
	}
	
	
	@Override
	public void fill(AbstractDataModel model, AbstractAttribute classAttr) throws Exception {
		if (!opened) return;
		
            
		if (attributes == null) {
			readAttributes();
		}

		String line;
			
		try {
			model.setAttributes(attributes);
			ArrayList<double[]> data = new ArrayList<double[]>();
			double[] vec;
			reset();
			while ( (line =  reader.readLine()) != null) {
				String[] vals = line.split(delimiter1);
				if (vals.length != attributes.size()) {
					System.out.println("Invalid count of attributes ");					
				} else {
					vec = new double[vals.length];
					for (int i = 0, j = 0; i < vals.length; i++) {
                                            if(i == 0){
						vec[j++] = attributes.get(i).toDouble(vals[i]);	
                                            } else{
                                                String[] values = vals[i].split(delimiter2);
                                                vec[j++] = attributes.get(i).toDouble(values[1]);
                                            }	
					}
					data.add(vec);					
				}				
			}
			model.setData(data, model.getAtttributeIndex(classAttr));
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public ArrayList<AbstractAttribute> readAttributes() {
		if (!opened) return null;

		attributes = new ArrayList<AbstractAttribute>();
		try {
			String line = reader.readLine();
			String[] vals = line.split(delimiter1);
			int detected = 0;
			for (int i = 0; i < vals.length; i++) {
				if (vals[i] == "")
					attributes.add(null);			
				else if(i == 0){
					try { 
						Double.parseDouble(vals[i]);
						attributes.add(new NumericAttribute( Integer.toString(i + 1) ));
					} catch (NumberFormatException e) {
						attributes.add(new CategorialAttribute(Integer.toString(i + 1)));						
					}
					++detected;
				} else {
                                          
                                                String[] values = vals[i].split(delimiter2);
                                                if(values[1] == null || values[1] == ""){                                                    
                                                    attributes.add(null);
                                                } else { 
                                                    try { 
                                                        Double.parseDouble(values[1]);
                                                        attributes.add(new NumericAttribute( Integer.toString(i + 1) ));
                                                    } catch (NumberFormatException e) {
                                                        attributes.add(new CategorialAttribute(Integer.toString(i + 1)));						
                                                    }
                                                    ++detected;
                                                }
                                
                                }
			}			
		} catch (Exception e) {
			
		}

		return attributes;
	}
	
	public void configure() {
		ConfigureSVMDataSourceDialog dlg = new ConfigureSVMDataSourceDialog(
		Environment.getEnvironment().getMainFrame(), this);
		dlg.setVisible(true);		
	}


	public String getDelimiter1() {
		return delimiter1;
	}

	public void setDelimiter1(String delimiter) {
		this.delimiter1 = delimiter;
	}
        
        public String getDelimiter2() {
		return delimiter2;
	}

	public void setDelimiter2(String delimiter) {
		this.delimiter2 = delimiter;
	}
	


	public String getQuickInfo() {
		StringBuilder sb = new StringBuilder();
		sb.append("SVM file data source" + Environment.newline);
		sb.append("File name:  " + filename + Environment.newline);
		sb.append("Delimiter1:  " + delimiter1 + Environment.newline);
                sb.append("Delimiter2:  " + delimiter2 + Environment.newline);
		return sb.toString();
	}
}




















